#ifndef RpcClient_h__
#define RpcClient_h__

#include <google/protobuf/service.h>
#include <google/protobuf/descriptor.h>
#include "../mythread/Atomic.h"
#include "../TcpConnector.h"

namespace myrpc
{
	
	struct RequetBody
	{	
		RequetBody() :_id(0), _done(nullptr) , _response(nullptr){}
		RequetBody(int id , 
					google::protobuf::Closure* done ,
					google::protobuf::Message* response) :_id(id), _done(done), _response(response) {}
		int _id;
		google::protobuf::Closure* _done;
		google::protobuf::Message* _response;
	};

	typedef std::shared_ptr<RequetBody> RequetBodyPtr;

	class RpcClient : public google::protobuf::RpcChannel
	{
	public:
		RpcClient() {
			tcphandle_ = std::make_shared<myrpc::mynet::TcpConnector>();
		}
		virtual ~RpcClient() = default;
		virtual void CallMethod(const google::protobuf::MethodDescriptor *method,
								google::protobuf::RpcController *controller,
								const google::protobuf::Message *request,
								google::protobuf::Message *response,
								google::protobuf::Closure *done);
		int Connect(std::string ip, int port);

		void SetIP(std::string ip)
		{
			ip_ = ip;
		}
		
		void SetPort(int port)
		{
			port = port;
		}

		void Active()
		{
			loop_->run();
		}

	private:
		static myrpc::mythread::Atomic atomic_;
		std::shared_ptr<myrpc::mynet::TcpConnector> tcphandle_;
		std::shared_ptr<uvw::Loop> loop_;
		std::string ip_;
		int port_;
		std::map<int, RequetBodyPtr> requestlist_;
	};
	
}
#endif // RpcClient_h__
